package com.ymatou.c2clivebizer.service;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;
/**
*视频直播搜索接口Call
*FreeMaker Template自动生成代码
*/
public class C2cCall extends YmatouBaseCall{
	private static String opurl ="search/c2c";
	public C2cCall() {
		super(EnvSetup.getSystemUrl("c2cLiveBizer"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	  // 获取status
	public String getStatus() {
		String status = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			status = jo.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}

	/**
	 * 获取total
	 * 
	 * @return int
	 */
	public int getTotal() {
		int total = 0;
		try {
			total = Integer.valueOf(new JSONObject(getReturnData())
					.getString("total"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return total;
	}
	
	
	/**
	 * 获取接口返回数据的记录
	 * */
	public JSONArray getHits(){
		JSONArray list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONArray("hits");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 返回stats里的数据
	 * */
	public String getStats(){
		String list = "no";
		
		try {
			JSONObject	jsonsList = new JSONObject(getReturnData());
			if(jsonsList.has("stats")==true){
				list = jsonsList.getString("stats");
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	/**
	 * 判断接口返回key中是否存在stats
	 * */
	public boolean statsRestul(){
		boolean statsRestul = false;
		try {
			JSONObject	jsonsList = new JSONObject(getReturnData());
			 statsRestul = jsonsList.has("stats");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statsRestul;
		
	}
	
	/**
	 * 获取hits列表
	 * 
	 * @return
	 */
	public ArrayList<HashMap<String, String>> getHitsList() {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		try {
			String rd = new JSONObject(getReturnData()).getString("hits");
			Gson gson = new Gson();
			list = gson.fromJson(rd, new TypeToken<ArrayList<HashMap<String, String>>>() {
			}.getType());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 得到接口公共返回值，如took，requestid，algorithm
	 * @return String 
	 * */
	public String getCommonInfo(String p) {
		String params = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			params = jo.getString(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
	
	/**
	 * 得到接口返回的boolean类型的参数
	 * @return boolean
	 * */
	public boolean getCommonInfoBoo(String p) {
		boolean paramBoolean = true;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			paramBoolean = jo.getBoolean(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramBoolean;
	}
	
	/*得到所有公共数据
	 * 
	 */
	public ArrayList getCommonArr(){
		
		ArrayList commonArr = new ArrayList();
		commonArr.add(getCommonInfo("status"));
		commonArr.add(getCommonInfo("took"));
		commonArr.add(getCommonInfo("requestid"));
		commonArr.add(getCommonInfoBoo("timeout"));
		commonArr.add(getCommonInfo("algorithm"));
		commonArr.add(getCommonInfo("hostip"));
		commonArr.add(getCommonInfo("downgrade"));
		
		return commonArr;
	}
	
	
	
}